<?php
if (!defined('INDEX') || INDEX == false) die ('Acc�s denied');

/**
 * \file sqld.php
 * \brief Driver MySQL for SQL control.
 * \author Bontiv
 *
 * Driver for MySQL extension.
 *
 * @version 0.1
 * \defgroup mysqld Lib MySQL Driver
 */

/**
 * \class sqld
 * \brief MySQL driver for SQL
 * 
 * lib_mysqld
 *
 * \ingroup mysqld
 */
class sqld {

	/// \brief Connection instance
	static protected $mysqldInstance = null;
	
	/// \brief Result array
	protected $mysqldResult;

	/**
	 * Send an Exeption if they aren't SQL Instance
	 */
	private function _needConnexion()
	{
		if (self::$mysqldInstance === null or self::$mysqldInstance === false)
		{
			throw new Exception('You try to send SQL Query without connexion');
		}
	}
    /**
     * Execute a SQL Query string $query and return an array
     * of result rows.
     * \brief Execute a query
     * @param string $query : An sql requet you want to execute
     * @return Array of associative Array.
     */
    protected function query($query)
    {
    	$this->_needConnexion();
    	$ret = array();
    	$this->mysqldResult = mysql_query($query, self::$mysqldInstance);
    	if ($this->mysqldResult === false || mysql_errno(self::$mysqldInstance) != 0)
    	{
    		throw new Exception('SQL Error (' . mysql_errno(self::$mysqldInstance) . ') : ' . mysql_error(self::$mysqldInstance) . ' Query : ' . $query);
    	}
    	elseif ($this->mysqldResult !== true)
    	{
    		while($row = mysql_fetch_assoc($this->mysqldResult)){
    			$ret[] = $row;
    		}
    		return $ret;
    	}
    }
    /**
     * Return nuber of rows in the result
     * \brief num rows affected
     * @return int nums rows
     */
    protected function nbRow()
    {
    	$this->_needConnexion();
    	if ($this->mysqldResult === true)
    	{
    		return mysql_affected_rows(self::$mysqldInstance);
    	}
    	elseif ($this->mysqldResult !== false)
		{
    		$num = mysql_num_rows($this->mysqldResult);
    		if ($num !== false)
    		{
    			return $num;
    		}
    		else
    		{
    			throw new Execption('SQL Engine Error');
    		}
    	}
    	else
    	{
    		return 0;
    	}
    }
    /**
     * \brief Connect to the MySQL Server
     * @param mixed $user User
     * @param mixed $pass Password
     * @param mixed $server MySQL server
     * @param mixed $port port of the server
     * @param mixed $database database name
     */
    static public function connect($user, $pass, $server, $port, $database)
    {
    	self::$mysqldInstance = @mysql_connect($server, $user, $pass);
    	if (self::$mysqldInstance === false)
    	{
    		throw new Exception('SQL Connexion Error');
    	}
    	else
    	{
    		if (mysql_select_db($database) === false)
    		{
    			self::$mysqldInstance = false;
    			throw new Exception('Database selection Error. Datbase exist ?');
    		}
    	}
    }
    /**
     * Escape a string to prepare it for query.
     * \brief Escape string
     * @param string $string what you want to escape.
     * @return string escaped string.
     */
    protected function escape($string)
    {
    	$this->_needConnexion();
    	return mysql_real_escape_string($string, self::$mysqldInstance);
    }
    /**
     * Free result memory
     * \brief Free result
     */
    protected function freeResult()
    {
    	@mysql_free_result($this->mysqldResult);
    }
    /**
     * \brief Close SQL connection.
     */
    static protected function close()
    {
    	@mysql_close(self::$mysqldInstance);
    }
    /**
     * \brief Return the last insert ID
     * @return int ID of the last insered data.
     */
    protected function lastID()
    {
    	$this->_needConnexion();
    	return mysql_insert_id(self::$mysqldInstance);
    }
	/**
	 * Return an associative array that associate key field with their tables
	 * of the last query.
	 * \brief Get pirmary Keys
	 * @return Array which associate primary key with his table.
	 */
	protected function getKey()
	{
		if ($this->mysqldResult == null)
		{
			throw new Exception('No query was send !');
		}
		else
		{
			$Keys = array();
			for ($i = 0; $i < mysql_num_fields($this->mysqldResult); $i++)
			{
				$field = mysql_fetch_field($this->mysqldResult, $i);
				if ($field->primary_key)
				{
					$Keys += Array($field->table => $field->name);
				}
			}
			return $Keys;
		}
	}
}

?>